<?
class pago_cxp{

	// Propiedades
	var $id;
	var $id_beneficiario;
	var $nombre_beneficiario;
	var $rif_beneficiario;
	var $id_zona;
	var $observaciones;
	var $fecha;
	var $deudas;
	var $pagos;

	var $total;

	function get($conn, $id){
		try {
			$q = "SELECT A.*, B.nombre AS nombre_beneficiario, B.rif AS rif_beneficiario FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id WHERE A.id = $id";
			//die($q);
			$r = $conn->Execute(utf8_encode($q));
			if(!$r->EOF){
				$this->id = $r->fields['id'];
				$this->id_beneficiario = $r->fields['id_beneficiario'];
				$this->nombre_beneficiario = $r->fields['nombre_beneficiario'];
				$this->rif_beneficiario = $r->fields['rif_beneficiario'];
				$this->id_zona = $r->fields['id_zona'];
				$this->observaciones = $r->fields['observaciones'];
				$this->fecha = $r->fields['fecha'];

				$q = "SELECT 1 AS sel, A.*, B.fecha AS fecha_cxp, B.factura AS factura_cxp, B.descripcion AS descripcion_cxp, B.saldo + A.monto AS saldo  FROM pagos_cxp_deudas AS A INNER JOIN egresos AS B ON A.id_cxp = B.id  WHERE A.id_pagos_cxp = $id";
				$rCD = $conn->Execute(utf8_encode($q));
				$j=0;

				while(!$rCD->EOF){
					$DeudasAux[$j][0] = $rCF->fields['sel'];
					$DeudasAux[$j][1] = $rCF->fields['id'];
					$DeudasAux[$j][2] = $rCF->fields['fecha_cxp'];
					$DeudasAux[$j][3] = $rCF->fields['factura_cxp'];
					$DeudasAux[$j][4] = $rCF->fields['descripcion_cxp'];
					$DeudasAux[$j][5] = $rCF->fields['saldo'];
					$DeudasAux[$j][6] = $rCF->fields['monto'];

					$j++;
					$rCD->movenext();
				}
				$this->deudas = new Services_JSON();
				$this->deudas = is_array($DeudasAux) ? $this->deudas->encode($DeudasAux) : false;


				$q = "SELECT A.*, C.nombre || ' - ' || B.numero AS cuenta FROM pagos_cxp_formas AS A LEFT JOIN cuentas_bancarias AS B ON A.id_cuenta = B.id LEFT JOIN bancos AS C ON B.id_banco = C.id WHERE A.id_pagos_cxp=$id";
				$rCF = $conn->Execute(utf8_encode($q));
				$i=0;

				while(!$rCF->EOF){
					$PagosAux[$i][0] = $rCF->fields['tipo_pago'];
					$PagosAux[$i][1] = $rCF->fields['id_cuenta'];
					$PagosAux[$i][2] = $rCF->fields['cuenta'];
					$PagosAux[$i][3] = $rCF->fields['referencia'];
					$PagosAux[$i][4] = $rCF->fields['cheque'];
					$PagosAux[$i][5] = $rCF->fields['monto'];

					$i++;
					$rCF->movenext();
				}
				$this->pagos = new Services_JSON();
				$this->pagos = is_array($PagosAux) ? $this->pagos->encode($PagosAux) : false;

			}


		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function get_all($conn, $orden="A.id DESC", $Tipo, $Valor, $TipoE, $max=10, $from=1){
		try {
			$zona = $_SESSION['SesionZona'];
			$TipoE = empty($TipoE) ? 0 : $TipoE;
			
			if(empty($Valor)){
				$q = "SELECT A.id FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id  ";
				$q.= " WHERE A.id_zona = $zona ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==0){
				$q = "SELECT A.id FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
				$q.= " WHERE A.id_zona = $zona  AND B.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			

			}
			elseif($Tipo==1){
				$q = "SELECT A.id FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
				$q.= " WHERE A.id_zona = $zona  AND B.rif::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==2){
				$q = "SELECT A.id FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
				$q.= "WHERE A.id_zona = $zona AND  A.fecha::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==3){
				$q = "SELECT A.id FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
				$q.= "WHERE A.id_zona = $zona AND A.observaciones::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}


			//die($q);			
			$r = ($max!=0) ? $conn->SelectLimit($q, $max, $from) : $conn->Execute(utf8_encode($q));
			$coleccion=array();
			while(!$r->EOF){
				$ue = new pago_cxp;
				$ue->get($conn, $r->fields['id']);
				$coleccion[] = $ue;
				$r->movenext();
			}
			$this->total = $r->RecordCount();
			return $coleccion;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}


	function add($conn, $id_beneficiario, $fecha, $observaciones, $deudas, $pagos){
		try {
			$zona = $_SESSION['SesionZona'];
			$q = "INSERT INTO pagos_cxp ";
			$q.= "(id_beneficiario, fecha, observaciones, id_zona) ";
			$q.= "VALUES ";
			$q.= "(NULLIF($id_beneficiario,-1), NULLIF('$fecha','')::date, upper('$observaciones'), $zona) ";
			//$q.= "RETURNING id; ";

			//die($q);
			$r = $conn->Execute(utf8_encode($q));

			$q2 = "SELECT MAX(id) AS id FROM pagos_cxp";

			$r2 = $conn->Execute(utf8_encode($q2));
			//$this->GuardarDeudas($conn, $r->fields['id'],$deudas);
			//$this->GuardarPagos($conn, $r->fields['id'],$pagos);

			$this->GuardarDeudas($conn, $r->fields['id'],$deudas);
			$this->GuardarPagos($conn, $r->fields['id'],$pagos);

			return REG_ADD_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function GuardarPagos($conn,$id,$pagos){
		try {
			$JsonRec = new Services_JSON();
			$JsonRec=$JsonRec->decode(str_replace("\\","",$pagos));
			$q = "DELETE FROM pagos_cxp_formas WHERE id_pagos_cxp=$id";
			$r = $conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			if(is_array($JsonRec->pagos)){
				foreach($JsonRec->pagos as $pagosAux){
					$monto = guardafloat($pagosAux[5]);
					$q = "INSERT INTO  pagos_cxp_formas ";
					$q.= "(id_pagos_cxp, tipo_pago, id_cuenta, referencia, cheque, monto) ";
					$q.= "VALUES ";
					$q.= "($id, $pagosAux[0], NULLIF('$pagosAux[1]','')::integer, upper('$pagosAux[3]'), $pagosAux[4] , $monto) ";
					
					//die($q);
					$conn->Execute(utf8_encode($q));
					$this->auditoria($conn, $q);
				}
			}
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function GuardarDeudas($conn,$id,$deudas){
		try {
			$JsonRec = new Services_JSON();
			$JsonRec=$JsonRec->decode(str_replace("\\","",$deudas));
			$q = "DELETE FROM pagos_cxp_deudas  WHERE id_pagos_cxp=$id";
			$r = $conn->Execute(utf8_encode($q));
			if(is_array($JsonRec->deudas)){
				foreach($JsonRec->deudas as $deudasAux){
					$monto = guardafloat($deudasAux[1]);
					$q = "INSERT INTO pagos_cxp_deudas(id_pagos_cxp, id_cxp, monto) VALUES($id, $deudasAux[0], $monto);";
					//die($q);
					$conn->Execute(utf8_encode($q));
				}
			}
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function del($conn, $id){
		try {
			$q = "DELETE FROM pagos_cxp WHERE id=$id";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_DEL_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function total_registro_busqueda($conn,$Tipo, $Valor, $TipoE, $orden="id")
	{
		$zona = $_SESSION['SesionZona'];
		$TipoE = empty($TipoE) ? 0 : $TipoE;
		if(empty($Valor)){
			$q = "SELECT count(*) AS total FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
			$q.= " WHERE A.id_zona = $zona";
		}
		elseif($Tipo==0){
			$q = "SELECT count(*) AS total FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
			$q.= " WHERE A.id_zona = $zona AND B.nombre::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==1){
			$q = "SELECT count(*) AS total FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
			$q.= " WHERE A.id_zona = $zona AND B.rif::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==2){
			$q = "SELECT count(*) AS total FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
			$q.= " WHERE A.id_zona = $zona AND A.fecha::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==3){
			$q = "SELECT count(*) AS total FROM pagos_cxp AS A INNER JOIN proveedores AS B ON A.id_beneficiario = B.id ";
			$q.= " WHERE A.id_zona = $zona AND A.observaciones::text ILIKE '%$Valor%'::text";
		}

		$r = $conn->Execute(utf8_encode($q));
		$total = $r->fields['total'];
		return $total;
	}
	
	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}	

	
}
?>
